import moment from "moment";
export const filterDate = (dateTime: number, fmt?: string): string => {
  if (!fmt) fmt = "YYYY-MM-DD HH:mm:ss";
  return moment(dateTime).format(fmt);
};
const formatterDate = (date: Date) => {
  let datetime =
    date.getFullYear() +
    "-" + // "年"
    (date.getMonth() + 1 > 9
      ? date.getMonth() + 1
      : "0" + (date.getMonth() + 1)) +
    "-" + // "月"
    (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
  return datetime;
};
const getToday = () => {
  let today = new Date();
  const arr = [];
  const day = formatterDate(today);
  arr.push(day + " 00:00:00");
  arr.push(day + " 23:59:59"); 

  return arr;
};
const getToMonth = () => {
  let time = new Date();
  let startTime = moment(time).date(1).startOf("month")
    .format("YYYY-MM-DD");
  let endTime = moment(time).date(1).endOf("month")
    .format("YYYY-MM-DD");
  return [
    startTime + " 00:00:00",
    endTime + " 23:59:59"
  ]
}

const getToWeek = () => {

  let startTime = moment().weekday(1).format('YYYY-MM-DD')
  let endTime = moment().weekday(7).format('YYYY-MM-DD')
  return [
    startTime + " 00:00:00",
    endTime + " 23:59:59"
  ]
}


export const timeGroupArr = () => {
  return [
    { label: "今天", value: getToday() },
    { label: "本周", value: getToWeek() },
    { label: "本月", value: getToMonth() }
  ];
};


const noArr = [0, 0, 0]
/**
 * 对比两个时间段的大小，不轮任何情况，默认插入 0时0分0秒，或位数不足，自动补0
 * @param start 开始时间
 * @param end 结束时间
 * @returns start >= end 若大于等于结束时间，代表结束时间是次日
 */
export const timeComparison = (start: string, end: string) => {
  let date = new Date();
  let startD = start.split(":").map(v => +v)
  let endD = end.split(":").map(v => +v)
  startD.push(...noArr)
  endD.push(...noArr)
  const newStart = date.setHours(startD[0], startD[1], startD[2])
  const newEnd = date.setHours(endD[0], endD[1], endD[2])
  console.log(newStart, newEnd)

  return newStart >= newEnd
}
/**
 * 
 * @param year 
 * @param month 
 * @returns 
 */
export function getMonthDay(year: number, month: number) {
  let days = new Date(year, month, 0).getDate()
  return days
}

export function formatTime(time: number): string {
  if (!time) return ''
  return moment(time).format('YYYY-MM-DD HH:mm:ss')
}